<template>
  <div class="app-container">
    <el-form ref="form">
      <el-form-item label="文件路径：">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-select
              placeholder="路径"
              size="small"
              style="width: 900px"
              v-model="path"
              @change="changeHandler"
            >
              <el-option
                v-for="path in confList"
                :key="path"
                :label="path"
                :value="path"
              />
            </el-select>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              style="left: 20px"
              size="mini"
              @click="saveHandler"
              >保存</el-button
            >
            <el-button type="primary" size="mini" @click="newHandler"
              >新建</el-button
            >
            <el-button type="primary" size="mini" @click="deleteHandler"
              >删除</el-button
            >
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="content"
          type="textarea"
          rows="32"
          placeholder="请输入内容"
        ></el-input>
      </el-form-item>
    </el-form>

    <el-dialog
      title="新建文件"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="newForm" :model="newForm" label-width="80px">
        <el-form-item label="路径">
          <el-select
            v-model="newForm.root"
            placeholder="路径"
            style="width: 100%"
          >
            <el-option
              v-for="root in newForm.rootDirs"
              :key="root"
              :label="root"
              :value="root"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文件名称">
          <el-input v-model="newForm.file"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="submit">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <second-confirm
      titleP="确认删除"
      labelP="是否删除"
      :msgP="msg"
      :openP="confirmOpen"
      @on-second-confirm="secondConfirmHandler"
    ></second-confirm>
  </div>
</template>
<script>
import {
  getConfList,
  getWorkerConfList,
  getConfRoot,
  getConfContent,
  saveConfContent,
  newConf,
  deleteConf,
} from "@/api/console";
import SecondConfirm from "@/components/SecondConfirm";

export default {
  components: { SecondConfirm },
  data() {
    return {
      loading: false,
      path: "",
      content: "",
      confList: [],
      open: false,
      newForm: {},
      confirmOpenP: false,
      msg: "",
    };
  },
  props: {
    sys: false,
    regionName: "",
    deployName: "",
  },
  created() {
    this.getConfList();
  },
  computed: {
    confirmOpen() {
      return this.confirmOpenP;
    },
  },
  methods: {
    getConfList() {
      this.loading = true;
      if (this.sys) {
        getConfList()
          .then((res) => {
            this.confList = res.data;
            this.getConfContent();
            this.loading = false;
          })
          .catch((msg) => {
            this.loading = false;
          });
      } else {
        if (!this.regionName || !this.deployName) {
          return;
        }
        getWorkerConfList(this.regionName, this.deployName)
          .then((res) => {
            this.confList = res.data;
            this.getConfContent();
            this.loading = false;
          })
          .catch((msg) => {
            this.loading = false;
          });
      }
    },
    getConfContent() {
      if (this.confList.length > 0) {
        this.path = this.confList[0];
        getConfContent(this.path).then((res) => {
          this.content = res.data;
        });
      }
    },
    changeHandler() {
      getConfContent(this.path).then((res) => {
        this.content = res.data;
      });
    },
    saveHandler() {
      if (this.path) {
        this.$confirm("是否确认保存?", "确定", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          saveConfContent(this.path, this.content)
            .then((res) => {
              this.msgSuccess("保存成功");
            })
            .catch(() => {});
        });
      } else {
        this.msgError("请选择文件");
      }
    },
    newHandler() {
      if (!this.sys && (!this.regionName || !this.deployName)) {
        return;
      }
      getConfRoot(this.sys, this.regionName, this.deployName).then((res) => {
        const rootDirs = res.data;
        if (rootDirs.length == 0) {
          return;
        } else {
          this.open = true;
          this.newForm = { file: "", rootDirs: rootDirs, root: rootDirs[0] };
        }
      });
    },
    submit() {
      newConf(
        this.sys,
        this.regionName,
        this.deployName,
        this.newForm.root + "/" + this.newForm.file
      )
        .then((res) => {
          this.open = false;
          this.newForm = { file: "" };
          this.msgSuccess("保存成功");
          this.getConfList();
        })
        .catch((msg) => {});
    },
    cancel() {
      this.newForm = {};
      this.open = false;
    },
    deleteHandler() {
      if (this.path) {
        this.confirmOpenP = true;
        this.msg = "确定要删除" + this.path + "?";
      } else {
        this.msgError("请选择文件");
      }
    },
    secondConfirmHandler(flag) {
      if (flag) {
        deleteConf(this.path)
          .then((res) => {
            this.confList.splice(
              this.confList.findIndex((p) => p === this.path),
              1
            );
            this.confirmOpenP = false;
            this.msgSuccess("删除成功");
            this.getConfContent();
          })
          .catch(() => {});
      } else {
        this.confirmOpenP = false;
      }
    },
  },
};
</script>